package com.stx.jeepay.payment.controller.refund;

import com.stx.jeepay.core.entity.RefundOrderEntity;
import com.stx.jeepay.core.expection.BizException;
import com.stx.jeepay.core.model.ApiRes;
import com.stx.jeepay.core.service.RefundOrderService;
import com.stx.jeepay.payment.controller.ApiController;
import com.stx.jeepay.payment.rqrs.refund.QueryRefundOrderRQ;
import com.stx.jeepay.payment.rqrs.refund.QueryRefundOrderRS;
import com.stx.jeepay.payment.service.ConfigContextQueryService;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商户退款单查询controller
 */
@Slf4j
@RestController
public class QueryRefundOrderController extends ApiController {
    @Autowired
    private RefundOrderService refundOrderService;
    @Autowired
    private ConfigContextQueryService configContextQueryService;

    /**
     * 查单接口
     **/
    @RequestMapping("/api/refund/query")
    public ApiRes queryRefundOrder() {
        // 获取参数
        QueryRefundOrderRQ rq = getRQByWithMchSign(QueryRefundOrderRQ.class);

        if (StringUtils.isAllEmpty(rq.getMchRefundNo(), rq.getRefundOrderId())) {
            throw new BizException("mchRefundNo 和 refundOrderId不能同时为空");
        }

        RefundOrderEntity refundOrder =
                refundOrderService.queryMchOrder(rq.getMchNo(), rq.getMchRefundNo(), rq.getRefundOrderId());
        if (refundOrder == null) {
            throw new BizException("订单不存在");
        }

        QueryRefundOrderRS bizRS = QueryRefundOrderRS.buildByRefundOrder(refundOrder);
        return ApiRes.okWithSign(bizRS, configContextQueryService.queryMchApp(rq.getMchNo(), rq.getAppId()).getAppSecret());
    }
}
